package com.handu.luna.core.domain;

import javax.annotation.PostConstruct;

import com.handu.luna.core.domain.goods.*;
import com.handu.luna.core.domain.goods.goodsMatch.GoodsMatchRepository;
import com.handu.luna.core.domain.order.OrderOperatorRepository;
import com.handu.luna.core.domain.promotion.*;
import com.handu.luna.core.domain.promotion.combo.ComboRepository;
import com.handu.luna.core.domain.system.DeliverAreaRepository;
import com.handu.luna.core.domain.system.DeliverRepository;
import com.handu.luna.core.domain.system.RegionRepository;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.handu.luna.core.domain.admin.AdminControllerCategoryRepository;
import com.handu.luna.core.domain.admin.AdminControllerDescriptionRepository;
import com.handu.luna.core.domain.admin.OperatorRepository;
import com.handu.luna.core.domain.admin.WorkLogRepository;
import com.handu.luna.core.domain.advertisement.AdPositionIdRepository;
import com.handu.luna.core.domain.advertisement.AdvertiseListRepository;
import com.handu.luna.core.domain.article.ArticleCategoryRepository;
import com.handu.luna.core.domain.article.ArticleListRepository;
import com.handu.luna.core.domain.order.OrderInfoRepository;
import com.handu.luna.core.domain.order.OrderDetailRepository;
import com.handu.luna.core.domain.promotion.CouponPromotionRepository;
import com.handu.luna.core.domain.promotion.CouponRepository;
import com.handu.luna.core.domain.promotion.CreditGoodsRepository;
import com.handu.luna.core.domain.promotion.CreditPromotionRepository;
import com.handu.luna.core.domain.promotion.SalesPromotionRepository;
import com.handu.luna.core.domain.promotion.TopicRepository;
import com.handu.luna.core.domain.support.EnumAwareConvertUtilsBean;
import com.handu.luna.core.domain.user.UserLogRepository;
import com.handu.luna.core.domain.user.UserRankRepository;
import com.handu.luna.core.domain.user.UserRepository;


@Component
public class Context {
	
	public enum Environment{
		DEVELOP("DEVELOP"),TEST("TEST"),PRODUCTION("PRODUCTION"),UNIT_TEST("UNIT_TEST");
		public String name;
		
		private Environment(String name){
			this.name = name;
		}
	}
	
	@Autowired
	private UserRepository userRepository;
	
	@Autowired
	private UserLogRepository userLogRepository;

	@Autowired
	private BrandRepository brandRepository;
	
	@Autowired
	private CategoryRepository categoryRepository;
	@Autowired
	private ArticleCategoryRepository articleCategoryRepository;
	@Autowired
	private CreditPromotionRepository creditPromotionRepository;

	@Autowired
	private CreditPromotionGoodsRepository creditPromotionGoodsRepository;
	@Autowired
	private ArticleListRepository articleListRepository;
	@Autowired
	private CreditGoodsGoodsRepository creditGoodsGoodsRepository;

    @Autowired
    private RegionRepository regionRepository;

    @Autowired
    private DeliverAreaRepository deliverAreaRepository;

    @Autowired
    private OrderOperatorRepository orderOperatorRepository;

    @Autowired
    private DeliverRepository deliverRepository;

    @Autowired
    private OperatorRepository operatorRepository;

    @Autowired
    private GoodsAttributeRepository goodsAttributeRepository;

    @Autowired
    private WorkLogRepository workLogRepository;

    @Autowired
    private TopicModuleRepository topicModuleRepository;

    @Autowired
    private CashCardLogRepository cashCardLogRepository;


    public CashCardLogRepository getCashCardLogRepository() {
        return cashCardLogRepository;
    }

    public void setCashCardLogRepository(CashCardLogRepository cashCardLogRepository) {
        this.cashCardLogRepository = cashCardLogRepository;
    }

    public TopicModuleRepository getTopicModuleRepository() {
        return topicModuleRepository;
    }

    public void setTopicModuleRepository(TopicModuleRepository topicModuleRepository) {
        this.topicModuleRepository = topicModuleRepository;
    }

    public WorkLogRepository getWorkLogRepository() {
		return workLogRepository;
	}

	public void setWorkLogRepository(WorkLogRepository workLogRepository) {
		this.workLogRepository = workLogRepository;
	}

	public GoodsAttributeRepository getGoodsAttributeRepository() {
        return goodsAttributeRepository;
    }

    public void setGoodsAttributeRepository(GoodsAttributeRepository goodsAttributeRepository) {
        this.goodsAttributeRepository = goodsAttributeRepository;
    }

    public OperatorRepository getOperatorRepository() {
		return operatorRepository;
	}

	public void setOperatorRepository(OperatorRepository operatorRepository) {
		this.operatorRepository = operatorRepository;
	}

	public DeliverRepository getDeliverRepository() {
        return deliverRepository;
    }

    public void setDeliverRepository(DeliverRepository deliverRepository) {
        this.deliverRepository = deliverRepository;
    }

    public OrderOperatorRepository getOrderOperatorRepository() {
        return orderOperatorRepository;
    }

    public void setOrderOperatorRepository(OrderOperatorRepository orderOperatorRepository) {
        this.orderOperatorRepository = orderOperatorRepository;
    }

    public DeliverAreaRepository getDeliverAreaRepository() {
        return deliverAreaRepository;
    }

    public void setDeliverAreaRepository(DeliverAreaRepository deliverAreaRepository) {
        this.deliverAreaRepository = deliverAreaRepository;
    }

    public RegionRepository getRegionRepository() {
        return regionRepository;
    }

    public void setRegionRepository(RegionRepository regionRepository) {
        this.regionRepository = regionRepository;
    }

    public CreditGoodsGoodsRepository getCreditGoodsGoodsRepository() {
		return creditGoodsGoodsRepository;
	}

	public void setCreditGoodsGoodsRepository(
			CreditGoodsGoodsRepository creditGoodsGoodsRepository) {
		this.creditGoodsGoodsRepository = creditGoodsGoodsRepository;
	}

	public ArticleListRepository getArticleListRepository() {
		return articleListRepository;
	}

	public void setArticleListRepository(ArticleListRepository articleListRepository) {
		this.articleListRepository = articleListRepository;
	}	
	
	public ArticleCategoryRepository getArticleCategoryRepository() {
		return articleCategoryRepository;
	}

	public void setArticleCategoryRepository(
			ArticleCategoryRepository articleCategoryRepository) {
		this.articleCategoryRepository = articleCategoryRepository;
	}


	public CreditPromotionGoodsRepository getCreditPromotionGoodsRepository() {
		return creditPromotionGoodsRepository;
	}

	public void setCreditPromotionGoodsRepository(
			CreditPromotionGoodsRepository creditPromotionGoodsRepository) {
		this.creditPromotionGoodsRepository = creditPromotionGoodsRepository;
	}



	@Autowired
	private AttributeRepository attributeRepository;

	@Autowired
	private BrandGeneralInfoRepository brandGeneralInfoRepository;
	
	@Autowired
	private UserRankRepository userRankRepository;
	
	@Autowired
	private GoodsRepository goodsRepository;
	
	@Autowired
	private SKURepository skuRepository;
	
	@Autowired
	private AlbumRepository albumRepository;
	
	@Autowired
	private SalesPromotionRepository salesPromotionRepository;
	
	@Autowired
	private SalesPromotionGoodsRepository salesPromotionGoodsRepository;
	
	@Autowired
	private SecKillGroupBuyRepository secKillGroupBuyRepository;
	
	@Autowired
	private SecKillGroupBuyGoodsRepository secKillGroupBuyGoodsRepository;
	
	@Autowired
	private ActivitiesRepository activitiesRepository;

	@Autowired
	private AdminControllerDescriptionRepository adminControllerDescriptionRepository;
	
	@Autowired
	private CreditGoodsRepository creditGoodsRepository;

	@Autowired
	private AdminControllerCategoryRepository adminControllerCategoryRepository;

    @Autowired
    private CouponPromotionRepository couponPromotionRepository;

    @Autowired
    private CouponRepository couponRepository;
    
    @Autowired
    private OrderDetailRepository orderDetailRepository;
    
    @Autowired
    private OrderInfoRepository orderInfoRepository;

    @Autowired
    private PostCardPromotionRepository postCardPromotionRepository;

    @Autowired
    private PostCardRepository postCardRepository;

    @Autowired
    private RedPacketPromotionRepository redPacketPromotionRepository;

    @Autowired
    private RedPacketRepository redPacketRepository;
    
    @Autowired
    private AdPositionIdRepository adPositionIdRepository;

    @Autowired
    private AdvertiseListRepository advertiseListRepository;
    
    @Autowired
    private PostForFreeActivityRepository  postForFreeActivityRepository;

    @Autowired
    private GoodsMatchRepository goodsMatchRepository;

    @Autowired
    private ComboRepository comboRepository;

    @Autowired
    private CashCardPromotionRepository cashCardPromotionRepository;

    @Autowired
    private CashCardRepository cashCardRepository;

    @Autowired
	private TopicRepository TopicRepository;

    public TopicRepository getTopicRepository() {
        return TopicRepository;
    }

    public void setTopicRepository(TopicRepository topicRepository) {
        TopicRepository = topicRepository;
    }

    public CashCardPromotionRepository getCashCardPromotionRepository() {
        return cashCardPromotionRepository;
    }

    public void setCashCardPromotionRepository(CashCardPromotionRepository cashCardPromotionRepository) {
        this.cashCardPromotionRepository = cashCardPromotionRepository;
    }

    public CashCardRepository getCashCardRepository() {
        return cashCardRepository;
    }

    public void setCashCardRepository(CashCardRepository cashCardRepository) {
        this.cashCardRepository = cashCardRepository;
    }

    public ComboRepository getComboRepository() {
        return comboRepository;
    }

    public void setComboRepository(ComboRepository comboRepository) {
        this.comboRepository = comboRepository;
    }

    public GoodsMatchRepository getGoodsMatchRepository() {
        return goodsMatchRepository;
    }

    public void setGoodsMatchRepository(GoodsMatchRepository goodsMatchRepository) {
        this.goodsMatchRepository = goodsMatchRepository;
    }

    public PostForFreeActivityRepository getPostForFreeActivityRepository() {
		return postForFreeActivityRepository;
	}

	public void setPostForFreeActivityRepository(
			PostForFreeActivityRepository postForFreeActivityRepository) {
		this.postForFreeActivityRepository = postForFreeActivityRepository;
	}

	public AdvertiseListRepository getAdvertiseListRepository() {
		return advertiseListRepository;
	}

	public void setAdvertiseListRepository(
			AdvertiseListRepository advertiseListRepository) {
		this.advertiseListRepository = advertiseListRepository;
	}

	public AdPositionIdRepository getAdPositionIdRepository() {
		return adPositionIdRepository;
	}

	public void setAdPositionIdRepository(
			AdPositionIdRepository adPositionIdRepository) {
		this.adPositionIdRepository = adPositionIdRepository;
	}

	public RedPacketPromotionRepository getRedPacketPromotionRepository() {
        return redPacketPromotionRepository;
    }

    public void setRedPacketPromotionRepository(RedPacketPromotionRepository redPacketPromotionRepository) {
        this.redPacketPromotionRepository = redPacketPromotionRepository;
    }

    public RedPacketRepository getRedPacketRepository() {
        return redPacketRepository;
    }

    public void setRedPacketRepository(RedPacketRepository redPacketRepository) {
        this.redPacketRepository = redPacketRepository;
    }

    public PostCardPromotionRepository getPostCardPromotionRepository() {
        return postCardPromotionRepository;
    }

    public void setPostCardPromotionRepository(PostCardPromotionRepository postCardPromotionRepository) {
        this.postCardPromotionRepository = postCardPromotionRepository;
    }

    public PostCardRepository getPostCardRepository() {
        return postCardRepository;
    }

    public AlbumRepository getAlbumRepository() {
		return albumRepository;
	}

	public void setAlbumRepository(AlbumRepository albumRepository) {
		this.albumRepository = albumRepository;
	}

	public void setPostCardRepository(PostCardRepository postCardRepository) {
        this.postCardRepository = postCardRepository;
    }

    public SalesPromotionGoodsRepository getSalesPromotionGoodsRepository() {
		return salesPromotionGoodsRepository;
	}

	public void setSalesPromotionGoodsRepository(
			SalesPromotionGoodsRepository salesPromotionGoodsRepository) {
		this.salesPromotionGoodsRepository = salesPromotionGoodsRepository;
	}

	public ActivitiesRepository getActivitiesRepository() {
		return activitiesRepository;
	}

	public void setActivitiesRepository(ActivitiesRepository activitiesRepository) {
		this.activitiesRepository = activitiesRepository;
	}

	public OrderDetailRepository getOrderDetailRepository() {
		return orderDetailRepository;
	}

	public void setOrderDetailRepository(OrderDetailRepository orderDetailRepository) {
		this.orderDetailRepository = orderDetailRepository;
	}

	public OrderInfoRepository getOrderInfoRepository() {
		return orderInfoRepository;
	}

	public void setOrderInfoRepository(OrderInfoRepository orderInfoRepository) {
		this.orderInfoRepository = orderInfoRepository;
	}

	public CouponRepository getCouponRepository() {
        return couponRepository;
    }

    public void setCouponRepository(CouponRepository couponRepository) {
        this.couponRepository = couponRepository;
    }

    public CouponPromotionRepository getCouponPromotionRepository() {
        return couponPromotionRepository;
    }

    public void setCouponPromotionRepository(CouponPromotionRepository couponPromotionRepository) {
        this.couponPromotionRepository = couponPromotionRepository;
    }

    public UserRankRepository getUserRankRepository() {
		return userRankRepository;
	}

	public void setUserRankRepository(UserRankRepository userRankRepository) {
		this.userRankRepository = userRankRepository;
	}
	
	public CategoryRepository getCategoryRepository() {
		return categoryRepository;
	}

	
	public void setCategoryRepository(CategoryRepository categoryRepository) {
		this.categoryRepository = categoryRepository;
	}

	public Environment getEnvironment() {
		return environment;
	}

	public void setEnvironment(Environment environment) {
		this.environment = environment;
	}

	public CreditGoodsRepository getCreditGoodsRepository() {
		return creditGoodsRepository;
	}

	public void setCreditGoodsRepository(CreditGoodsRepository creditGoodsRepository) {
		this.creditGoodsRepository = creditGoodsRepository;
	}

	public UserRepository getUserRepository() {
		return userRepository;
	}

	public void setUserRepository(UserRepository userRepository) {
		this.userRepository = userRepository;
	}

	public BrandRepository getBrandRepository() {
		return brandRepository;
	}

	public void setBrandRepository(BrandRepository brandRepository) {
		this.brandRepository = brandRepository;
	}
	
	
	public AttributeRepository getAttributeRepository() {
		return attributeRepository;
	}

	public void setAttributeRepository(AttributeRepository attributeRepository) {
		this.attributeRepository = attributeRepository;
	}


	public BrandGeneralInfoRepository getBrandGeneralInfoRepository() {
		return brandGeneralInfoRepository;
	}

	public void setBrandGeneralInfoRepository(
			BrandGeneralInfoRepository brandGeneralInfoRepository) {
		this.brandGeneralInfoRepository = brandGeneralInfoRepository;
	}

	public GoodsRepository getGoodsRepository() {
		return goodsRepository;
	}

	public void setGoodsRepository(GoodsRepository goodsRepository) {
		this.goodsRepository = goodsRepository;
	}
	
	public SalesPromotionRepository getSalesPromotionRepository() {
		return salesPromotionRepository;
	}

	public void setSalesPromotionRepository(
			SalesPromotionRepository salesPromotionRepository) {
		this.salesPromotionRepository = salesPromotionRepository;
	}

	public SecKillGroupBuyRepository getSecKillGroupBuyRepository() {
		return secKillGroupBuyRepository;
	}

	public void setSecKillGroupBuyRepository(
			SecKillGroupBuyRepository secKillGroupBuyRepository) {
		this.secKillGroupBuyRepository = secKillGroupBuyRepository;
	}

	public SecKillGroupBuyGoodsRepository getSecKillGroupBuyGoodsRepository() {
		return secKillGroupBuyGoodsRepository;
	}

	public void setSecKillGroupBuyGoodsRepository(
			SecKillGroupBuyGoodsRepository secKillGroupBuyGoodsRepository) {
		this.secKillGroupBuyGoodsRepository = secKillGroupBuyGoodsRepository;
	}

	public AdminControllerDescriptionRepository
    getAdminControllerDescriptionRepository() {
		return adminControllerDescriptionRepository;
	}

	public void setAdminControllerDescriptionRepository(
			AdminControllerDescriptionRepository adminControllerDescriptionRepository) {
		this.adminControllerDescriptionRepository = adminControllerDescriptionRepository;
	}

	public AdminControllerCategoryRepository getAdminControllerCategoryRepository() {
		return adminControllerCategoryRepository;
	}

	public void setAdminControllerCategoryRepository(
			AdminControllerCategoryRepository adminControllerCategoryRepository) {
		this.adminControllerCategoryRepository = adminControllerCategoryRepository;
	}

	public CreditPromotionRepository getCreditPromotionRepository() {
		return creditPromotionRepository;
	}

	public void setCreditPromotionRepository(
			CreditPromotionRepository creditPromotionRepository) {
		this.creditPromotionRepository = creditPromotionRepository;
	}


	public UserLogRepository getUserLogRepository() {
		return userLogRepository;
	}

	public void setUserLogRepository(UserLogRepository userLogRepository) {
		this.userLogRepository = userLogRepository;
	}

	public SKURepository getSkuRepository() {
		return skuRepository;
	}

	public void setSkuRepository(SKURepository skuRepository) {
		this.skuRepository = skuRepository;
	}

	public static final String SYSTEM_ENV_FLAG ="LUNA_ENV";
	public Environment environment = Environment.DEVELOP;
	
	public static Context current;
	
	/**
	 * 全局初始化，包括内容
	 * 1、初始化环境变量，分为开发环境、测试环境和生产环境，使用环境变量中LUNA_ENV的标识区分，如设置为LUNA_ENV=TEST，则为测试环境
	 * 2、设置apache commons中BeanUtils转换枚举类型的方法
	 * 3、初始化数据
	 */
	@PostConstruct
	public void init(){
		Context.current = this;
		Context.initEnvironment(this);
		BeanUtilsBean.setInstance(new BeanUtilsBean(new EnumAwareConvertUtilsBean()));
		
		if((this.environment == Environment.TEST || this.environment == Environment.DEVELOP) && this.operatorRepository.count() == 0){
			new Seed(this).init();
		}
	}
	
	public static void initEnvironment(Context context){
		if(context.environment != Environment.UNIT_TEST && System.getenv().containsKey(SYSTEM_ENV_FLAG)){
			if(Environment.DEVELOP.name.equals(System.getenv().get(SYSTEM_ENV_FLAG))){
				context.environment = Environment.DEVELOP;
			}else if(Environment.TEST.name.equals(System.getenv().get(SYSTEM_ENV_FLAG))){
				context.environment = Environment.TEST;
			}else if(Environment.PRODUCTION.name.equals(System.getenv().get(SYSTEM_ENV_FLAG))){
				context.environment = Environment.PRODUCTION;
			}			
		}		
	}
}
